//初始化商品页渲染数据
localStorage.setItem('switch1', 'flower')
//检测本地存储是否开启
var te = localStorage.getItem('arr')
if(te==null){
	var arr=[]
	localStorage.setItem('arr',JSON.stringify(arr))
}
//获取用户名框对象
var userName = document.querySelector('.xb-2 input')
//获取密码框对象
var passWord = document.querySelector('.xb-3 input')
//获取登陆按钮对象
var denglu = document.querySelector('.xb-4')
//获取注册按钮对象
var zhuce = document.querySelector('.xb-6')
//获取导航栏用户名对象
var uName = document.querySelector('.h-right li:nth-child(1)')
// //获取记住密码对象
// var jizhu1 = document.querySelector('.jizhu input')
// console.log(denglu)
// console.log(jizhu1.checked)

//用户记忆功能
//登陆功能
denglu.onclick=function(){
	var s1 = parseInt(userName.value)
	var reg1 = /^\d{4}$/
	var s2 = passWord.value
	var reg2=/^[a-zA-z]\d{5}$/
	//用户名验证
	if(reg1.test(s1)){
		//密码验证
		if(reg2.test(s2)){
			//匹配本都存储用户名和密码
			var name = localStorage.getItem('user1')//获取用户名
			var pass = localStorage.getItem('pass1')//获取密码
			console.log(name)
			//比较输入用户名密码正确与否
			if(userName.value==name){
				if(passWord.value==pass){
					alert(`登陆成功！欢迎您！${name}!`)
					uName.innerHTML=`<a href="login.html">您好,${name}! </a><a>&nbsp;退出</a>`
					localStorage.setItem('rem',1)//设置记录用户
					shoppcar.children[3].innerHTML=localStorage.getItem('carnum')
					window.location.href="index.html"
				}else{
				alert('密码错误！')
				}
			}else{
				alert('用户名不存在！')
				}
		}else{
		     alert('密码输入不正确,请输入首位是字母的6位数')
			 passWord.focus()
		}
	}else{
	     alert('用户名输入不正确,请输入4位纯数字')
		 userName.focus()
	}
}
//注册功能(用户名密码并存储)
zhuce.onclick=function(){
	var s1 = parseInt(userName.value)
	var reg1 = /^\d{4}$/
	var s2 = passWord.value
	var reg2=/^[a-zA-z]\d{5}$/
	//用户名验证
	if(reg1.test(s1)){
		//密码验证
		if(reg2.test(s2)){
			//如果用户名相同
			var olduse = localStorage.getItem('user1')//获取用户名
			olduse=parseInt(olduse)
			console.log(s1)
			if(olduse==s1){
				alert('用户名已被占用！不可用！')
			}else{
				alert('注册成功！请登录！')
				localStorage.setItem('user1',userName.value)//设置用户名
				localStorage.setItem('pass1',passWord.value)//设置密码	
			}
			
		}else{
			alert('密码格式错误')
		}	
	}else{
		alert('用户名格式错误')
	}
}


//全局点击事件
var uName = document.querySelector('.h-right li:nth-child(1)')//获取导航栏用户名对象
var shoppcar = document.querySelector('.h-right li:nth-child(9)')//购物车商品数量
var name = localStorage.getItem('user1')//获取用户名
var rem1 = localStorage.getItem('rem')//获取用户记忆
var num1 = localStorage.getItem('carnum')
if(rem1=='1'){
		uName.innerHTML=`<a href="login.html">您好,${name}! </a><a>&nbsp;退出</a>`
	}
shoppcar.children[3].innerHTML=localStorage.getItem('carnum')

//通用，页面跳转
document.onclick=function(e){
	var e = e||window.event
	var target = e.target || e.srcElement
	//跳转商品列表页
	if(target.innerHTML=="永生花"|| target.innerHTML=="鲜花"){
		window.location.href="listPage.html"
	}
	//跳转登录注册页
	if(target.innerHTML=="注册||您好，请登录"){
		window.location.href="login.html"
	}
	//跳转商品详情页
	if(  target.parentNode.parentNode.className=="goods-show"){
		var str=target.parentNode.parentNode.children[0].children[0].src
		var miao1=target.parentNode.parentNode.children[1].children[0].innerHTML
		var miao2=target.parentNode.parentNode.children[1].children[2].innerHTML
		miao2=miao2.split('￥')
		miao2=miao2[1]
		str=str.split('flower/')
		var str1=str[1]
		var str2=`../${str1}`
		
		localStorage.setItem('str2',str2)//设置当前图片地址(原图)
		localStorage.setItem('str1',str1)//设置当前图片地址(放大图)
		localStorage.setItem('miao1',miao1)//设置当前商品标题(原图)
		localStorage.setItem('miao2',miao2)//设置当前商品价格(放大图)
		window.location.href="detailPages.html"
	}
	
	if(target.parentNode.parentNode.className=="down-r-block"){
		// console.log(target.parentNode.parentNode.children[1].children[1].innerHTML)
		var str=target.parentNode.parentNode.children[0].children[0].src
		var miao1=target.parentNode.parentNode.children[1].children[0].innerHTML
		var miao2=target.parentNode.parentNode.children[1].children[1].innerHTML
		miao2=miao2.split('￥')
		miao2=miao2[1]
		str=str.split('flower/')
		var str1=str[1]
		var str2=`../${str1}`
		
		localStorage.setItem('str2',str2)//设置当前图片地址(原图)
		localStorage.setItem('str1',str1)//设置当前图片地址(放大图)
		localStorage.setItem('miao1',miao1)//设置当前商品标题(原图)
		localStorage.setItem('miao2',miao2)//设置当前商品价格(放大图)
		window.location.href="detailPages.html"
	}
	//跳转首页
	if(target.innerHTML=="首页"){
		window.location.href="index.html"
	}
	//跳转购物车页
	if(target.parentNode.className=="guowu"){
		var check1=localStorage.getItem('rem')
		if(check1==null){
			alert('请先登录！查看购物车！')
			window.location.href="login.html"
		}else{
			 window.location.href="shopping.html"
			}
	}
	//退出用户登录
	if(target.innerHTML=="&nbsp;退出"){
		if(confirm("退出后购物车将被清空，请问是否退出？")){
			localStorage.removeItem('rem')
			localStorage.removeItem('str1')
			localStorage.removeItem('str2')
			localStorage.removeItem('miao1')
			localStorage.removeItem('miao2')
			localStorage.setItem('carnum','0')
			var arr=[]
			localStorage.setItem('arr',JSON.stringify(arr))
			window.location.reload()
		}
	}
	//加入购物车
	if(target.innerHTML=="加入购物车"){
		//检验是否登陆
		var check1=localStorage.getItem('rem')
		if(check1==null){
			alert('请先登录！查看购物车！')
			window.location.href="login.html"
		}else{
			num1++
			localStorage.setItem('carnum',num1)
			//找到本页导航购物车物品数量值
			
			shoppcar.children[3].innerHTML=localStorage.getItem('carnum')
			 window.location.href="shopping.html"
			}
		
	}
	//继续挑选
	if(target.innerHTML=="继续挑选"){
		window.location.href="listPage.html"
	}
}